package com.mybatisplus.service.impl;

import com.mybatisplus.mapper.StudentMapper;
import com.mybatisplus.mapper.TeacherMapper;
import com.mybatisplus.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;

/**
 * Auto created By IntelliJ IDEA
 *
 * @author : gzrong
 * @Date : 2023/5/7 13:10
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    TeacherMapper teacherMapper;
    @Autowired
    StudentMapper studentMapper;

    @Override
    public boolean login(String stuid, String pwd, int usertype, HttpServletResponse response) {
        boolean pass = false;
        System.out.println("stuid:" + stuid +
                "\npwd:" + pwd +
                "\nusertype:" + usertype);
        switch (usertype) {
            case 1: {
                if (teacherMapper.checkUserExists(stuid, pwd)) {
                    response.setHeader("LoginStatus", "1");//设置重定向URL，URL为主页
                    pass = true;
//            response.setHeader("REDIRECT", "REDIRECT");//设定响应头为重定向类型
                } else {
                    response.setHeader("LoginStatus", "-1");//设置重定向URL并添加参数，URL为登录页面
//            response.setHeader("REDIRECT", "REDIRECT");//设置响应头为重定向
                }
                break;
            }
            case 2: {
                if (studentMapper.checkUserExists(stuid, pwd)) {
                    response.setHeader("LoginStatus", "2");//设置重定向URL，URL为主页
//            response.setHeader("REDIRECT", "REDIRECT");//设定响应头为重定向类型
                    pass = true;
                } else {
                    response.setHeader("LoginStatus", "-1");//设置重定向URL并添加参数，URL为登录页面
//            response.setHeader("REDIRECT", "REDIRECT");//设置响应头为重定向
                }
                break;
            }
        }
        return pass;
    }
}
